/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.agilelang.generate;

import org.agilelang.object.ObjectClass;
import org.agilelang.object.ObjectClassAttribute;
import org.agilelang.object.ObjectFunction;
import org.agilelang.object.ObjectParam;
import org.agilelang.object.ObjectProject;
import org.agilelang.object.Read;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.swing.JTextArea;

/**
 *
 * @author Roderick
 */
public class AgileGeneratorOld {



    public void process(String path_xml, String path_tpl, String path_out, List<ObjectClass> lists_config, JTextArea jtext) {
        ObjectProject agileproject = null;
        String suf = "//";
        String functions = "";
        String attributes = "";

        String sqlCreate = "INSERT INTO //models//(//fileds_split_comma//) values (//fileds_values_split_comma//)";
        String sqlRead = "SELECT //fileds_split_comma// FROM //models// WHERE id = //id//";
        String sqlUpdate = "UPDATE  //models// //fileds_egual_value_split_comma// WHERE id = //id//";
        String sqlDelete = "DELETE FROM //models// WHERE id = //id//";
        String sqlSelect = "SELECT //fileds_split_comma// FROM //models//";
        String sqlValues = "//fileds_split_comma//";

        Read rd = new Read();
        agileproject = rd.process(path_xml);

        //ReadConfig rc = new ReadConfig();
        //ConfigProject cp = rc.process(path_config);
        for (ObjectClass co : lists_config) {

            for (ObjectClass cls : agileproject.getClasses()) {
                ObjectClass config = null;
                if (co.getType().equals(cls.getType())) {
                    config = co;
                    //break;
                }

                if (config == null) {
                    continue;
                }
                // Atributos de la clase   
                for (ObjectFunction fun : config.getFunctions()) {
                    if (fun.getName().equals("defineTemplate")) {
                        
                        String filename = "", ext = "",template="";
                        for (ObjectParam attr : fun.getParams()) {
                            if (attr.getName().equals("filename")) {
                                filename = attr.getValue();
                            }
                            if (attr.getName().equals("ext")) {
                                ext = attr.getValue();
                            }
                             if (attr.getName().equals("template")) {
                                template = attr.getValue();
                            }
                        }
                        
                        Partial tpl_file = new Partial(path_tpl + File.separator + template);
                        for (ObjectClassAttribute attr : cls.getClassattributes()) {
                            if (attr.getName().equals("name")) {
                                tpl_file.replace(suf + "names" + suf, Inflector.getInstance().pluralize(cls.getName()).toLowerCase());
                                tpl_file.replace(suf + "NAMES" + suf, Inflector.getInstance().pluralize(cls.getName()).toUpperCase());
                                tpl_file.replace(suf + "Names" + suf, Inflector.getInstance().capitalize(Inflector.getInstance().pluralize(cls.getName())));
                                tpl_file.replace(suf + "name" + suf, Inflector.getInstance().singularize(cls.getName()).toLowerCase());
                                tpl_file.replace(suf + "NAME" + suf, Inflector.getInstance().singularize(cls.getName()).toUpperCase());
                                tpl_file.replace(suf + "Name" + suf, Inflector.getInstance().capitalize(Inflector.getInstance().singularize(cls.getName())));
                            } else {
                                tpl_file.replace(suf + attr.getName() + suf, attr.getValue());
                                /*tpl_file.replace(suf + Inflector.getInstance().pluralize(attr.getName()).toLowerCase() + suf, Inflector.getInstance().pluralize(attr.getValue()).toLowerCase());
                                 tpl_file.replace(suf + Inflector.getInstance().pluralize(attr.getName()).toUpperCase() + suf, Inflector.getInstance().pluralize(attr.getValue()).toUpperCase());
                                 tpl_file.replace(suf + Inflector.getInstance().capitalize(Inflector.getInstance().pluralize(attr.getName())) + suf, Inflector.getInstance().capitalize(Inflector.getInstance().pluralize(attr.getValue())));
                                 tpl_file.replace(suf + Inflector.getInstance().singularize(attr.getName()).toLowerCase() + suf, Inflector.getInstance().singularize(attr.getValue()).toLowerCase());
                                 tpl_file.replace(suf + Inflector.getInstance().singularize(attr.getName()).toUpperCase() + suf, Inflector.getInstance().singularize(attr.getValue()).toUpperCase());
                                 tpl_file.replace(suf + Inflector.getInstance().capitalize(Inflector.getInstance().singularize(attr.getName())) + suf, Inflector.getInstance().capitalize(Inflector.getInstance().singularize(attr.getValue())));
                                 */
                            }
                        }

                //--------------------------------------------------------------
                        // funciones
                        
                        Map<String, String> replace = new HashMap<>();
                        for (ObjectFunction con_fun : config.getFunctions()) {
                            int index = 0;
                            for (ObjectFunction fun1 : cls.getFunctions()) {

                                //if (con_fun.getName().equals(fun.getName())) {
                                if (con_fun.getName().equals("addAttribute")) {

                                    if (con_fun.getDefine("type").equals(fun1.getName())) {

                                        Partial tpl_function = new Partial(path_tpl + File.separator + con_fun.getDefine("template"));
                                        tpl_function.replace(suf + "index" + suf, String.valueOf(index));
                                        index++;
                                        for (ObjectParam attr : fun1.getParams()) {
                                            if (attr.getName().equals("name")) {
                                        //tpl_function.replace(suf + "name_function" + suf, camel(attr.getValue()));
                                                //tpl_function.replace(suf + attr.getName() + suf, attr.getValue());

                                                tpl_function.replace(suf + "names" + suf, Inflector.getInstance().pluralize(attr.getValue()).toLowerCase());
                                                tpl_function.replace(suf + "NAMES" + suf, Inflector.getInstance().pluralize(attr.getValue()).toUpperCase());
                                                tpl_function.replace(suf + "Names" + suf, Inflector.getInstance().capitalize(Inflector.getInstance().pluralize(attr.getValue())));
                                                tpl_function.replace(suf + "name" + suf, Inflector.getInstance().singularize(attr.getValue()).toLowerCase());
                                                tpl_function.replace(suf + "NAME" + suf, Inflector.getInstance().singularize(attr.getValue()).toUpperCase());
                                                tpl_function.replace(suf + "Name" + suf, Inflector.getInstance().capitalize(Inflector.getInstance().singularize(attr.getValue())));

                                            } else {

                                                tpl_function.replace(suf + attr.getName() + suf, attr.getValue());
                                                /*tpl_function.replace(suf + Inflector.getInstance().pluralize(attr.getName()).toLowerCase() + suf, Inflector.getInstance().pluralize(attr.getValue()).toLowerCase());
                                                 tpl_function.replace(suf + Inflector.getInstance().pluralize(attr.getName()).toUpperCase() + suf, Inflector.getInstance().pluralize(attr.getValue()).toUpperCase());
                                                 tpl_function.replace(suf + Inflector.getInstance().capitalize(Inflector.getInstance().pluralize(attr.getName())) + suf, Inflector.getInstance().capitalize(Inflector.getInstance().pluralize(attr.getValue())));
                                                 tpl_function.replace(suf + Inflector.getInstance().singularize(attr.getName()).toLowerCase() + suf, Inflector.getInstance().singularize(attr.getValue()).toLowerCase());
                                                 tpl_function.replace(suf + Inflector.getInstance().singularize(attr.getName()).toUpperCase() + suf, Inflector.getInstance().singularize(attr.getValue()).toUpperCase());
                                                 tpl_function.replace(suf + Inflector.getInstance().capitalize(Inflector.getInstance().singularize(attr.getName())) + suf, Inflector.getInstance().capitalize(Inflector.getInstance().singularize(attr.getValue())));
                                                 */
                                            }
                                        }
                                        tpl_function.replace(suf + "clsnames" + suf, Inflector.getInstance().pluralize(cls.getName()).toLowerCase());
                                        tpl_function.replace(suf + "CLSNAMES" + suf, Inflector.getInstance().pluralize(cls.getName()).toUpperCase());
                                        tpl_function.replace(suf + "Clsnames" + suf, Inflector.getInstance().capitalize(Inflector.getInstance().pluralize(cls.getName())));
                                        tpl_function.replace(suf + "clsname" + suf, Inflector.getInstance().singularize(cls.getName()).toLowerCase());
                                        tpl_function.replace(suf + "CLSNAME" + suf, Inflector.getInstance().singularize(cls.getName()).toUpperCase());
                                        tpl_function.replace(suf + "Clsname" + suf, Inflector.getInstance().capitalize(Inflector.getInstance().singularize(cls.getName())));

                                        //functions += tpl_function.getPtl();
                                        if (replace.containsKey(con_fun.getDefine("replace"))) {
                                            String value = replace.get(con_fun.getDefine("replace"));
                                            value += tpl_function.getPtl();
                                            replace.put(con_fun.getDefine("replace"), value);
                                        } else {
                                            String value = tpl_function.getPtl();
                                            replace.put(con_fun.getDefine("replace"), value);
                                        }
                                    }

                                }
                            }
                        }

                        for (ObjectFunction con_fun : config.getFunctions()) {
                            for (ObjectFunction fun1 : cls.getFunctions()) {

                                if (con_fun.getName().equals(fun1.getName())) {
                                    if (con_fun.getDefine("type").equals("function")) {

                                        Partial tpl_function = new Partial(path_tpl + File.separator + con_fun.getDefine("template"));
                                        for (ObjectParam attr : fun1.getParams()) {
                                            if (attr.getName().equals("name")) {
                                        //tpl_function.replace(suf + "name_function" + suf, camel(attr.getValue()));
                                                //tpl_function.replace(suf + attr.getName() + suf, attr.getValue());

                                                tpl_function.replace(suf + "names" + suf, Inflector.getInstance().pluralize(attr.getValue()).toLowerCase());
                                                tpl_function.replace(suf + "NAMES" + suf, Inflector.getInstance().pluralize(attr.getValue()).toUpperCase());
                                                tpl_function.replace(suf + "Names" + suf, Inflector.getInstance().capitalize(Inflector.getInstance().pluralize(attr.getValue())));
                                                tpl_function.replace(suf + "name" + suf, Inflector.getInstance().singularize(attr.getValue()).toLowerCase());
                                                tpl_function.replace(suf + "NAME" + suf, Inflector.getInstance().singularize(attr.getValue()).toUpperCase());
                                                tpl_function.replace(suf + "Name" + suf, Inflector.getInstance().capitalize(Inflector.getInstance().singularize(attr.getValue())));

                                            } else {

                                                //tpl_function.replace(suf + attr.getName() + suf, attr.getValue());
                                                tpl_function.replace(suf + Inflector.getInstance().pluralize(attr.getName()).toLowerCase() + suf, Inflector.getInstance().pluralize(attr.getValue()).toLowerCase());
                                                tpl_function.replace(suf + Inflector.getInstance().pluralize(attr.getName()).toUpperCase() + suf, Inflector.getInstance().pluralize(attr.getValue()).toUpperCase());
                                                tpl_function.replace(suf + Inflector.getInstance().capitalize(Inflector.getInstance().pluralize(attr.getName())) + suf, Inflector.getInstance().capitalize(Inflector.getInstance().pluralize(attr.getValue())));
                                                tpl_function.replace(suf + Inflector.getInstance().singularize(attr.getName()).toLowerCase() + suf, Inflector.getInstance().singularize(attr.getValue()).toLowerCase());
                                                tpl_function.replace(suf + Inflector.getInstance().singularize(attr.getName()).toUpperCase() + suf, Inflector.getInstance().singularize(attr.getValue()).toUpperCase());
                                                tpl_function.replace(suf + Inflector.getInstance().capitalize(Inflector.getInstance().singularize(attr.getName())) + suf, Inflector.getInstance().capitalize(Inflector.getInstance().singularize(attr.getValue())));

                                            }
                                        }
                                        tpl_function.replace(suf + "clsnames" + suf, Inflector.getInstance().pluralize(cls.getName()).toLowerCase());
                                        tpl_function.replace(suf + "CLSNAMES" + suf, Inflector.getInstance().pluralize(cls.getName()).toUpperCase());
                                        tpl_function.replace(suf + "Clsnames" + suf, Inflector.getInstance().capitalize(Inflector.getInstance().pluralize(cls.getName())));
                                        tpl_function.replace(suf + "clsname" + suf, Inflector.getInstance().singularize(cls.getName()).toLowerCase());
                                        tpl_function.replace(suf + "CLSNAME" + suf, Inflector.getInstance().singularize(cls.getName()).toUpperCase());
                                        tpl_function.replace(suf + "Clsname" + suf, Inflector.getInstance().capitalize(Inflector.getInstance().singularize(cls.getName())));

                                        //functions += tpl_function.getPtl();
                                        if (replace.containsKey(con_fun.getDefine("replace"))) {
                                            String value = replace.get(con_fun.getDefine("replace"));
                                            value += tpl_function.getPtl();
                                            replace.put(con_fun.getDefine("replace"), value);
                                        } else {
                                            String value = tpl_function.getPtl();
                                            replace.put(con_fun.getDefine("replace"), value);
                                        }
                                    }

                                    if (con_fun.getDefine("type").equals("view")) {
                                        String str_file = "";
                                        Partial tpl_function = new Partial(path_tpl + File.separator + con_fun.getDefine("template"));
                                        for (ObjectParam attr : fun1.getParams()) {
                                            tpl_function.replace(suf + attr.getName() + suf, attr.getValue());
                                            if (attr.getName().equals("name")) {
                                                str_file = attr.getValue();
                                            }
                                        }
                                        tpl_function.replace(suf + "clsnames" + suf, Inflector.getInstance().pluralize(cls.getName()).toLowerCase());
                                        tpl_function.replace(suf + "CLSNAMES" + suf, Inflector.getInstance().pluralize(cls.getName()).toUpperCase());
                                        tpl_function.replace(suf + "Clsnames" + suf, Inflector.getInstance().capitalize(Inflector.getInstance().pluralize(cls.getName())));
                                        tpl_function.replace(suf + "clsname" + suf, Inflector.getInstance().singularize(cls.getName()).toLowerCase());
                                        tpl_function.replace(suf + "CLSNAME" + suf, Inflector.getInstance().singularize(cls.getName()).toUpperCase());
                                        tpl_function.replace(suf + "Clsname" + suf, Inflector.getInstance().capitalize(Inflector.getInstance().singularize(cls.getName())));

                                        for (Map.Entry mapEntry : replace.entrySet()) {
                                            tpl_function.replace(suf + mapEntry.getKey() + suf, mapEntry.getValue().toString());
                                        }
                                        UtilFile.writefile(str_file + con_fun.getDefine("ext"), path_out, tpl_function.getPtl());
                                    }
                                }
                            }
                        }

                        String fileds_split_comma = "", fileds_values_split_comma = "";
                        String fileds_split_comma_read = "";
                        String fileds_split_comma_update = "";

                        for (ObjectFunction fun1 : cls.getFunctions()) {

                            for (ObjectParam attr : fun1.getParams()) {

                                if (attr.getName().equals("name")) {

                                    fileds_split_comma += attr.getValue();
                                    fileds_values_split_comma += "?, ";
                                    fileds_split_comma += ", ";

                                    //----------------------------------------------
                                    fileds_split_comma_read += attr.getValue();
                                    fileds_split_comma_read += ", ";
                                    //----------------------------------------------
                                    fileds_split_comma_update += attr.getValue() + " = ?";
                                    fileds_split_comma_update += ", ";
                                }
                            }

                        }

                        if (fileds_split_comma.length() > 0) {
                            sqlCreate = sqlCreate.replace("//models//", name_file("files", cls.getName()));
                            sqlCreate = sqlCreate.replace("//fileds_split_comma//", fileds_split_comma.substring(0, fileds_split_comma.length() - 2));
                            sqlCreate = sqlCreate.replace("//fileds_values_split_comma//", fileds_values_split_comma.substring(0, fileds_values_split_comma.length() - 2));
                            //----------------------------------------------------------
                            sqlRead = sqlRead.replace("//models//", name_file("files", cls.getName()));
                            sqlRead = sqlRead.replace("//fileds_split_comma//", fileds_split_comma_read.substring(0, fileds_split_comma_read.length() - 2));
                            sqlRead = sqlRead.replace("//id//", "?");
                            //----------------------------------------------------------
                            sqlUpdate = sqlUpdate.replace("//models//", name_file("files", cls.getName()));
                            sqlUpdate = sqlUpdate.replace("//fileds_egual_value_split_comma//", fileds_split_comma_update.substring(0, fileds_split_comma_update.length() - 2));
                            sqlUpdate = sqlUpdate.replace("//id//", "?");
                            //----------------------------------------------------------
                            sqlDelete = sqlDelete.replace("//models//", name_file("files", cls.getName()));
                            sqlDelete = sqlDelete.replace("//id//", "?");
                            //----------------------------------------------------------
                            sqlSelect = sqlSelect.replace("//models//", name_file("files", cls.getName()));
                            sqlSelect = sqlSelect.replace("//fileds_split_comma//", fileds_split_comma.substring(0, fileds_split_comma.length() - 2));
                            sqlSelect = sqlSelect.replace("//id//", "?");
                            //----------------------------------------------------------
                            sqlValues = sqlValues.replace("//models//", name_file("files", cls.getName()));
                            sqlValues = sqlValues.replace("//fileds_split_comma//", fileds_split_comma.substring(0, fileds_split_comma.length() - 2));
                            sqlValues = sqlValues.replace("//id//", "?");
                        }
                        tpl_file.replace(suf + "sqlCreate" + suf, sqlCreate);
                        tpl_file.replace(suf + "sqlRead" + suf, sqlRead);
                        tpl_file.replace(suf + "sqlUpdate" + suf, sqlUpdate);
                        tpl_file.replace(suf + "sqlDelete" + suf, sqlDelete);
                        tpl_file.replace(suf + "sqlSelect" + suf, sqlSelect);
                        tpl_file.replace(suf + "sqlValues" + suf, sqlValues);
                        for (Map.Entry mapEntry : replace.entrySet()) {
                            tpl_file.replace(suf + mapEntry.getKey() + suf, mapEntry.getValue().toString());
                        }

                        /*UtilFile.writefile(name_file(config.getDefineTemplate("filename"), cls.getName()) + config.getDefineTemplate("ext"), path_out, tpl_file.getPtl());
                         jtext.append(path_out + File.separator + name_file(config.getDefineTemplate("filename"), cls.getName()) + config.getDefineTemplate("ext"));
                         jtext.append("\n");*/
                        

                        UtilFile.writefile(name_file(filename, cls.getName()) + ext, path_out, tpl_file.getPtl());
                        jtext.append(path_out + File.separator + name_file(filename, cls.getName()) + ext);
                        jtext.append("\n");
                    }
                }
            }
        }
    }

    
        public String name_file(String config, String name) {
        switch (config) {
            case "files":
                return Inflector.getInstance().pluralize(name).toLowerCase();
            case "FILES":
                return Inflector.getInstance().pluralize(name).toUpperCase();
            case "Files":
                return Inflector.getInstance().capitalize(Inflector.getInstance().pluralize(name));
            case "file":
                return Inflector.getInstance().singularize(name).toLowerCase();
            case "FILE":
                return Inflector.getInstance().singularize(name).toUpperCase();
            case "File":
                return Inflector.getInstance().capitalize(Inflector.getInstance().singularize(name));
        }
        return "";
    }
    

    public String camel(String name) {
        return Inflector.getInstance().capitalize(name);
    }
    public static void main(String ar[]) {
        String path_xml = "E:\\agile\\AgileLang\\Test\\out.xml";
        String path_tpl = "E:\\agile\\AgileLang\\Test\\tmpl_java";
        String path_out = "E:\\agile\\AgileLang\\Test\\out";
        String path_config = "E:\\agile\\AgileLang\\Test\\config_java.xml";
        AgileGeneratorOld ag = new AgileGeneratorOld();
        //ag.process(path_xml, path_tpl, path_out, path_config, new JTextArea());
    }

}
